<?php

namespace app\system\controller\daily;

use app\common\controller\System;
use ba\Filesystem;
use hehe\Str;
use hehe\Trade;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Db;

/**
 * 商品管理
 */
class Goods extends System {
    /**
     * Goods模型对象
     * @var object
     * @phpstan-var \app\admin\model\goods\Goods
     */
    protected object $model;

    protected string|array $defaultSortField = 'id,desc';

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = ['name', 'brand.name'];

    public function initialize(): void {
        parent::initialize();
        $this->model = new \app\system\model\daily\Goods;
    }


    /**
     * 下单
     */
    public function placeOrder(): void {
        $param = $this->request->param();
        if(empty($param['goods_url_id'])) $this->error('请选择购买链接');
        if(empty($param['num'])) $this->error('请输入购买数量');
        if(empty($param['sell_total_price'])) $this->error('请输入出售总价');
        if(empty($param['buy_total_price'])) $this->error('请输入购买总价');
        if($param['pay_type'] < 0) $this->error('请选择付款方式');

        db::startTrans();
        try{
            $order_id = db::name('daily_order')->where(['status' => 0])->value('id');
            if(!$order_id){
                $order_insert = [
                    'out_trade_no' => Trade::generateTradeNo(),
                    'money' => 0,
                    'suborder_num' => 0,
                    'create_time' => $this->timestamp
                ];
                $order_id = db::name('daily_order')->insertGetId($order_insert);
            }
            $suborder_insert = [
                'order_id' => $order_id,
                'goods_id' => $param['id'],
                'num' => $param['num'],
                'goods_url_id' => $param['goods_url_id'],
                'sell_total_price' => $param['sell_total_price'],
                'buy_total_price' => $param['buy_total_price'],
                'pay_type' => $param['pay_type'],
                'create_time' => $this->timestamp
            ];
            db::name('daily_suborder')->insert($suborder_insert);
            db::commit();
        }catch(\Exception $e){
            db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();
    }

    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list' => $res->items(),
            'total' => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }


}